<?php
/**
 * Created by PhpStorm.
 * User: czm
 * Date: 2019-07-17
 * Time: 15:51
 */
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;

class Card extends Controller
{
    public function cardList()
    {
        $id = input('id');
        if(Db::name('card')->where('id',$id)->select()){
            $da = ['code' => 200, 'msg' => 'OK'];
            return json_encode($da);
        }else{
            $da = ['code' => 403, 'msg' => '失败'];
            return json_encode($da,TRUE);
        }
    }

    public function cardAdd()
    {
        if (request()->isPost())
        {
            $data = input('post.');
            $a  = $data['name'];
            $aa = Db::name('card')->where('name',$a)->find();
            if ($aa){
                $dat = ['code' => 431, 'msg' => '用户存在'];
                return json_encode($dat, TRUE);
            }
            $ab = $data['phone'];
            $abb = Db::name('card')->where('phone',$ab)->find();
            if ($abb){
                $dat = ['code' => 403, 'msg' => '手机号存在'];
                return json_encode($dat, TRUE);
            }
            // 头像
            if ($_FILES['pic']['tmp_name'])
            {
                $file=request()->file('pic');//文件上传
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/images');//移动文件，DS代表/或者\斜杠
                $data['pic']=$info->getSaveName();//图片的保存路径
            }
            // logo
            if ($_FILES['logo']['tmp_name'])
            {
                $file=request()->file('logo');//文件上传
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/images');//移动文件，DS代表/或者\斜杠
                $data['logo']=$info->getSaveName();//图片的保存路径
            }
            $res = Db::name('card')->insertGetId($data);
            if ($res)
            {
                $dat = ['code' => 200, 'msg' => '添加成功'];
                return json_encode($dat, TRUE);
            }else{
                $dat = ['code' => 403, 'msg' => '注册失败'];
                return json_encode($dat, TRUE);
            }
        }

    }

    public function cardPerfecting()
    {
        if (request()->isPost()) {
            if (!Session::get('name') && !Session::get('id')) {
                $dat = ['code' => 403, 'msg' => '完善信息错误'];
                return json_encode($dat, TRUE);
            }
            $data = input('post.');
            $data['uid'] = Session::get('id');
            if ($_FILES['productpic']['tmp_name']) {
                $file = request()->file('productpic');//文件上传
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/images');//移动文件，DS代表/或者\斜杠
                $data['productpic'] = $info->getSaveName();//图片的保存路径
            }
            $r = Db::name('card_perfecting')->insert($data);
            if ($r){
                $dat = ['code' => 200, 'msg' => '成功'];
                return json_encode($dat, TRUE);
            }else{
                $dat = ['code' => 403, 'msg' => '失败'];
                return json_encode($dat, TRUE);
            }
        }

    }

    public function cardDetails()
    {
        $data = input('post.');
        $a  = $data['name'];
        $aa = Db::name('card')->where('name',$a)->find();
        if($aa)
        {
            $data = ['code'=>200,'msg'=>'success',$aa];
            return json_encode($data,JSON_FORCE_OBJECT);
        }else {
            $data = ['code' => 403, 'msg' => 'error', $aa];
            return json_encode($data, TRUE);
        }

    }

    public function cardPurchase()
    {
        if (request()->isPost()){
//            $dat = input('post.');
//            $a = $dat['id'];
            $da = [
//                'uid'   => $a,
                'name' => input('name'),
                'remarks'    =>  input('remarks'),
                'telephone' =>  input('telephone'),
                'WeChat'    =>  input('WeChat'),
            ];
            if (Db::name('purchase')->insert($da))
                {
                    $data = ['code'=>200,'msg'=>'success'];
                    return json_encode($data,JSON_FORCE_OBJECT);
                }else {
                    $data = ['code' => 403, 'msg' => 'error'];
                    return json_encode($data, TRUE);
                }
        }
    }

    public function cardDel()
    {
        $id = input('id');
        $r = Db::name('card')->where('id',$id)->delete($id);
        if ($r){
            $da = ['code' => 200, 'msg' => '删除成功'];
            return json_encode($da);
        }else{
            $da = ['code' => 403, 'msg' => '失败'];
            return json_encode($da,TRUE);
        }
    }
}